﻿// zxLoginDialog.cpp: 实现文件
//

#include "stdafx.h"
#include "zxLoginDialog.h"
#include "afxdialogex.h"

#include "resource.h"

// CLoginDialog 对话框

IMPLEMENT_DYNAMIC(zxLoginDialog, CDialogEx)

zxLoginDialog::zxLoginDialog(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_LOGIN, pParent)
	, m_strUser(_T(""))
	, m_strPasswd(_T(""))
{

}

zxLoginDialog::~zxLoginDialog()
{
}

void zxLoginDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_USER, m_strUser);
	DDX_Text(pDX, IDC_EDIT_PASSWORD, m_strPasswd);
	DDX_Control(pDX, IDC_STATIC_TITLE, m_wndTitle);
}


BEGIN_MESSAGE_MAP(zxLoginDialog, CDialogEx)
	ON_WM_ERASEBKGND()
	ON_BN_CLICKED(IDOK, &zxLoginDialog::OnBnClickedOk)
	ON_WM_CTLCOLOR()
END_MESSAGE_MAP()


// CLoginDialog 消息处理程序


BOOL zxLoginDialog::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	m_bgBitmap.LoadBitmap(IDB_BITMAP_LOGIN_BACKGROUND);

	m_strUser = "admin";
	m_strPasswd = "admin";
	UpdateData(FALSE);

	m_font.CreateFont(32, // nHeight 
		0, // nWidth 
		0, // nEscapement 
		0, // nOrientation 
		FW_NORMAL, // nWeight 
		FALSE, // bItalic 
		FALSE, // bUnderline 
		0, // cStrikeOut 
		ANSI_CHARSET, // nCharSet 
		OUT_DEFAULT_PRECIS, // nOutPrecision 
		CLIP_DEFAULT_PRECIS, // nClipPrecision 
		DEFAULT_QUALITY, // nQuality 
		DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily 
		_T("SimHei")); // lpszFac

	m_wndTitle.SetFont(&m_font);

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}


BOOL zxLoginDialog::OnEraseBkgnd(CDC* pDC)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if (m_bgBitmap.GetSafeHandle())
	{
		CRect rect;
		GetClientRect(rect);

		CDC memDC;
		memDC.CreateCompatibleDC(pDC);
		CBitmap* pOldBitmap = memDC.SelectObject(&m_bgBitmap);

		BITMAP bmp;
		m_bgBitmap.GetBitmap(&bmp);

		pDC->StretchBlt(
			0, 0, rect.Width(), rect.Height(),
			&memDC,
			0, 0, bmp.bmWidth, bmp.bmHeight,
			SRCCOPY);

		memDC.SelectObject(pOldBitmap);
	}

	//return CDialogEx::OnEraseBkgnd(pDC);
	return TRUE;
}


void zxLoginDialog::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	m_strUser.Trim();
	m_strPasswd.Trim();

	if ((m_strUser == "admin") && (m_strPasswd == "admin"))
	{
		CDialogEx::OnOK();
	}
	else
	{
		if (m_strUser != "admin")
		{
			AfxMessageBox("用户名错误");
		}
		else if (m_strPasswd == "admin")
		{
			AfxMessageBox("密码错误");
		}
	}

	CDialogEx::OnOK();
}

HBRUSH zxLoginDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);

	// TODO:  在此更改 DC 的任何特性
	switch (pWnd->GetDlgCtrlID())
	{
	case IDC_STATIC:
	case IDC_STATIC_TITLE:
		pDC->SetBkMode(TRANSPARENT);
		pDC->SetTextColor(RGB(0, 0, 0));
		return (HBRUSH)GetStockObject(HOLLOW_BRUSH);
	default:
		break;
	}
	return hbr;
}
